import { defineStore } from 'pinia'
import { findRoles } from '@/api/permission'

export const useRolesStore = defineStore({
  id: 'roles',
  state: () => ({
    roles: []
  }),
  actions: {
    async initRoles() {
      const userInfo =
        localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo'))

      const res = await findRoles()

      if (res.code === 200) {
        const rolesList = res.data

        this.roles = rolesList
          .filter((role) => userInfo.roles.includes(role.id))
          .map((role) => role.name)

        if (userInfo.username === 'admin') this.roles = ['admin']
      }
    }
  }
})
